using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Ent.Web
{
    public class Javascript
    {
        public static string ReadAllText(string path, string[] replacement)
        {
            string retValue = "";
            if (File.Exists(path))
            {
                retValue = File.ReadAllText(path);
                if (replacement == null)
                    return retValue;
                for (int i = 0; i < replacement.Length; i++)
                {
                    retValue = retValue.Replace("$" + Convert.ToInt32(i + 1), replacement.GetValue(i).ToString());
                }
            }
            return retValue;
        }
        public static string CreateFunction(string name, string[] statements)
        {
            string retValue = "\r\nfunction " + name + "()";
            retValue += "\r\n{";
            for (int i = 0; i < statements.Length; i++)
            {
                retValue += "\r\n\t" + statements.GetValue(i).ToString();
            }
            retValue += "\r\n}";
            return retValue;
        }
        public static string CheckInteger(string chkVariable, string setVariable)
        { 
            string jScript="";
            jScript += "var cnt;";
            jScript += "var s=" + chkVariable + ";";
            jScript += "for (cnt = 0; cnt < s.length; cnt++)";
            jScript += "{";
            jScript += "var c = s.charAt(cnt);";
            jScript += "if (!((c >= '0') && (c <= '9')))" + setVariable + "=false;";
            jScript += "}";
            return jScript;
        }
    }
}
